package nameless.aistudy.api.auth;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import nameless.aistudy.api.auth.dto.JwtToken;
import nameless.aistudy.common.constant.path.DispatchPathConstants;
import nameless.aistudy.common.exception.BusinessException;
import nameless.aistudy.common.result.Result;
import nameless.aistudy.common.util.jwt.JwtUtil;
import nameless.aistudy.domain.model.user.User;
import nameless.aistudy.domain.service.user.UserAuthService;
import nameless.aistudy.domain.service.user.dto.LoginRequest;
import nameless.aistudy.domain.service.user.dto.RegisterRequest;
import nameless.aistudy.domain.service.user.dto.UserSendCodeRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static nameless.aistudy.common.constant.path.DispatchPathConstants.AUTH_FORBIDDEN_PATH;
import static nameless.aistudy.common.enumeration.message.MessageEnum.*;


@RestController
public class AuthController {

    @Autowired
    private UserAuthService userAuthService;

    @Operation(
            summary = "发送验证码短信"
    )
    @PostMapping("/user/send-code")
    public Result<String> sendCode(@RequestBody UserSendCodeRequest request) {
        try {
            userAuthService.sendVerificationCode(request.getPhone());
            return Result.success(AUTH_CODE_SENT.getMessage());
        } catch (BusinessException e) {
            throw new BusinessException(AUTH_CODE_SEND_FAILED.getMessage() + "（" + e.getMessage() + "）");
        }
    }

    @Operation(
            summary = "用户注册"
    )
    @PostMapping("/user/register")
    public Result<String> register(@RequestBody RegisterRequest request) {
        // 验证手机验证码
        try {
            userAuthService.register(request);
            return Result.success(REGISTER_SUCCESS.getMessage());
        } catch (BusinessException e) {
            throw new BusinessException(REGISTER_FAILED.getMessage() + "（" + e.getMessage() + "）");
        }
    }

    @PostMapping("/user/login")
    public Result<JwtToken> login(@RequestBody LoginRequest request) {
        User user = userAuthService.login(request.getUsernameOrPhone(), request.getPassword());

        JwtToken token = new JwtToken();
        token.setToken(JwtUtil.generateToken(user.getId().toString()));

        return Result.success(LOGIN_SUCCESS.getMessage(), token);
    }

    @RequestMapping(DispatchPathConstants.AUTH_UNAUTHORIZED_PATH)
    public ResponseEntity<Result<String>> unauthorized(HttpServletRequest request) {
        String message = (String) request.getAttribute("message");
        Result<String> result = Result.fail(HttpStatus.UNAUTHORIZED, message);
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
    }

    @RequestMapping(DispatchPathConstants.AUTH_FORBIDDEN_PATH)
    public ResponseEntity<Result<String>> forbidden(HttpServletRequest request) {
        String message = (String) request.getAttribute("message");
        Result<String> result = Result.fail(HttpStatus.FORBIDDEN, message);
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(result);
    }

}
